<?php 
class ControllerProductCat extends Controller {  
	public function index() { 
		$this->language->load('product/cat');
	
		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
      		'href'      => HTTP_SERVER . 'index.php?route=common/home',
       		'text'      => $this->language->get('text_home'),
       		'separator' => FALSE
   		);	

		$this->load->model('catalog/cat');
		$this->load->model('tool/seo_url');  
		
		if (isset($this->request->get['paths'])) {
			$paths = '';
		
			$parts = explode('_', $this->request->get['paths']);
		
			foreach ($parts as $paths_id) {
				$cat_info = $this->model_catalog_cat->getCat($paths_id);
				
				if ($cat_info) {
					if (!$paths) {
						$paths = $paths_id;
					} else {
						$paths .= '_' . $paths_id;
					}

	       			$this->document->breadcrumbs[] = array(
   	    				'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $paths),
    	   				'text'      => $cat_info['name'],
        				'separator' => $this->language->get('text_separator')
        			);
				}
			}		
		
			$cat_id = array_pop($parts);
		} else {
			$cat_id = 0;
		}
		
		$cat_info = $this->model_catalog_cat->getCat($cat_id);
	
		if ($cat_info) {
	  		$this->document->title = $cat_info['name'];
			
			$this->document->keywords = $cat_info['meta_keywords'];
			
			$this->document->description = $cat_info['meta_description'];
			
			$this->data['heading_title'] = $cat_info['name'];
			
			$root=$this->getRootParent($cat_info['cat_id']);
			$this->data['root_name']="";
			if($root!=$cat_info['cat_id']){
				$root_info=$this->model_catalog_cat->getCat($root);
				if($root_info){
					$this->data['root_name']=$root_info['name'];
					$this->data['root_href']= $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $root_info['cat_id']);
				}
			}
			
			$this->data['description'] = html_entity_decode($cat_info['description'], ENT_QUOTES, 'UTF-8');
			
			$this->data['text_sort'] = $this->language->get('text_sort');
			
			if (isset($this->request->get['page'])) {
				$page = $this->request->get['page'];
			} else { 
				$page = 1;
			}	
			
			if (isset($this->request->get['sort'])) {
				$sort = $this->request->get['sort'];
			} else {
				$sort = 'p.date_available';
			}

			if (isset($this->request->get['order'])) {
				$order = $this->request->get['order'];
			} else {
				$order = 'DESC';
			}

			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->load->model('catalog/article');  
			
			$this->load->model('tool/image'); 
			 
			$cat_total = $this->model_catalog_cat->getTotalCatsByCatId($cat_id);
			$article_total = $this->model_catalog_article->getTotalArticlesByCatId($cat_id);
			$this->data['articles'] = array();
			if ($cat_total || $article_total) {
        		$this->data['cats'] = array();
        		
				$results = $this->model_catalog_cat->getCats($cat_id);
				if($results){
	        		foreach ($results as $result) {
						if ($result['image']) {
							$image = $result['image'];
						} else {
							$image = 'no_image.jpg';
						}
						
						$this->data['cats'][] = array(
							'cat_id'=>$result['cat_id'],
	            			'name'  => $result['name'],
	            			'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $this->request->get['paths'] . '_' . $result['cat_id'] . $url),
	            			'thumb' => $this->model_tool_image->resize($image, $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'))
	          			);
	        		
	        		
						$results2 = $this->model_catalog_article->getArticlesByCatId($result['cat_id'], $sort, $order, ($page - 1) * $this->config->get('config_catalog_limit'), $this->config->get('config_catalog_limit'));
						
		        		foreach ($results2 as $result) {
							if ($result['image']) {
								$image = $result['image'];
							} else {
								$image = 'no_image.jpg';
							}
							
							
							
							$this->data['articles'][$result['cat_id']][] = array(
		            			'name'    => $result['name'],
								'model'   => $result['model'],
								'meta_description'=>$result['meta_description'],								         			
								'thumb'   => $this->model_tool_image->resize($image, $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
								'href'    => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/article&paths=' . $this->request->get['paths'].'_' . $result['cat_id'] . '&article_id=' . $result['article_id']),					
								'date_available' => $this->translateDate(date('l d-m-Y', strtotime($result['date_available'])))
		          			);
		        		}
		        		
	        		}
				}else{
				$results2 = $this->model_catalog_article->getArticlesByCatId($cat_id, $sort, $order, ($page - 1) * $this->config->get('config_catalog_limit'), $this->config->get('config_catalog_limit'));
						
		        		foreach ($results2 as $result) {
							if ($result['image']) {
								$image = $result['image'];
							} else {
								$image = 'no_image.jpg';
							}
							
							
							
							$this->data['articles'][] = array(
		            			'name'    => $result['name'],
								'model'   => $result['model'],
								'meta_description'=>$result['meta_description'],
								'image'	  => $this->model_tool_image->resize($image,300,250),	            			
								'thumb'   => $this->model_tool_image->resize($image, $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
								'href'    => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/article&paths=' . $this->request->get['paths'] . '&article_id=' . $result['article_id']),					
								'date_available' =>$this->translateDate(date('l d-m-Y', strtotime($result['date_available'])))
		          			);
		        		}
				}
				$this->load->model('catalog/review');
				
				$this->data['button_add_to_cart'] = $this->language->get('button_add_to_cart');
				
				

				
		
				$url = '';
		
				if (isset($this->request->get['page'])) {
					$url .= '&page=' . $this->request->get['page'];
				}			
		
				$this->data['sorts'] = array();
				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_default'),
					'value' => 'p.sort_order-ASC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $this->request->get['paths'] . '&sort=p.sort_order&order=ASC')
				);
				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_name_asc'),
					'value' => 'pd.name-ASC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $this->request->get['paths'] . '&sort=pd.name&order=ASC')
				);
 
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_name_desc'),
					'value' => 'pd.name-DESC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $this->request->get['paths'] . '&sort=pd.name&order=DESC')
				);  

				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_price_asc'),
					'value' => 'p.price-ASC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $this->request->get['paths'] . '&sort=p.price&order=ASC')
				); 

				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_price_desc'),
					'value' => 'p.price-DESC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $this->request->get['paths'] . '&sort=p.price&order=DESC')
				); 
				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_rating_desc'),
					'value' => 'rating-DESC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $this->request->get['paths'] . '&sort=rating&order=DESC')
				); 
				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_rating_asc'),
					'value' => 'rating-ASC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $this->request->get['paths'] . '&sort=rating&order=ASC')
				); 			
				
				$url = '';
		
				if (isset($this->request->get['sort'])) {
					$url .= '&sort=' . $this->request->get['sort'];
				}	

				if (isset($this->request->get['order'])) {
					$url .= '&order=' . $this->request->get['order'];
				}
			
				$pagination = new Pagination();
				$pagination->total = $article_total;
				$pagination->page = $page;
				$pagination->limit = $this->config->get('config_catalog_limit');
				$pagination->text = $this->language->get('text_pagination');
				$pagination->url = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $this->request->get['paths'] . $url . '&page={page}');
			
				$this->data['pagination'] = $pagination->render();
			
				$this->data['sort'] = $sort;
				$this->data['order'] = $order;
			
				if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/cat.tpl')) {
					$this->template = $this->config->get('config_template') . '/template/product/cat.tpl';
				} else {
					$this->template = 'default/template/product/cat.tpl';
				}	
				
				$this->children = array(
					'common/column_right',
					'common/column_left',
					'common/footer',
					'common/header'
				);
		
				$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));										
      		} else {
        		$this->document->title = $cat_info['name'];
				
				$this->document->description = $cat_info['meta_description'];
				
        		$this->data['heading_title'] = $cat_info['name'];

        		$this->data['text_error'] = $this->language->get('text_empty');

        		$this->data['button_continue'] = $this->language->get('button_continue');

        		$this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';
		
				$this->data['cats'] = array();
				
				$this->data['articles'] = array();
						
				if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/cat.tpl')) {
					$this->template = $this->config->get('config_template') . '/template/product/cat.tpl';
				} else {
					$this->template = 'default/template/product/cat.tpl';
				}	
				
				$this->children = array(
					'common/column_right',
					'common/column_left',
					'common/footer',
					'common/header'
				);
		
				$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
      		}
    	} else {
			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
				
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}	
			
			if (isset($this->request->get['paths'])) {	
	       		$this->document->breadcrumbs[] = array(
   	    			'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $this->request->get['paths'] . $url),
    	   			'text'      => $this->language->get('text_error'),
        			'separator' => $this->language->get('text_separator')
        		);
			}
				
			$this->document->title = $this->language->get('text_error');

      		$this->data['heading_title'] = $this->language->get('text_error');

      		$this->data['text_error'] = $this->language->get('text_error');

      		$this->data['button_continue'] = $this->language->get('button_continue');

      		$this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';
			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}	
			
			$this->children = array(
				'common/column_right',
				'common/column_left',
				'common/footer',
				'common/header'
			);
		
			$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
		}
  	}
public function translateDate($date){
		$strday='';
		$day='';
		
		if(isset($date)){
			$array=split(" ",$date);		
			$day=$array[0];
			$date=$array[1];
			if(isset($day)){
				switch ($day){
					case "Monday":
						$strday='Thứ Hai';
						break;
					case "Tuesday":
						$strday='Thứ Ba';
						break;
					case "Wednesday":
						$strday='Thứ Tư';
						break;
					case "‎Thursday":
						$strday='Thứ Năm';
						break;
					case "Friday":
						$strday='Thứ Sáu';
						break;
					case "Saturday":
						$strday='Thứ Bảy';
						break;
					case "Sunday":
						$strday='Chủ Nhật';
						break;	
				}
			}
		}
		return $strday." ".$date;
	}
	protected function getRootParent($cat_id){
		$result = $this->model_catalog_cat->getCat($cat_id);
		if($result['parent_id']!=0){
			$root=$this->getRootParent($result['parent_id']);
		}else{
			$root=$result['cat_id'];
		}
		return $root;
	}
}
?>